package zhuyun.code.launcheractivity.view.widget

import android.content.Context
import android.content.res.TypedArray
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.animation.AnimationUtils
import android.widget.RelativeLayout
import kotlinx.android.synthetic.main.et_img_hint.view.*
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.utils.UIUitls

/**
 * @author: Administrator
 * @date: 2021/11/17
 */
class ImgHintEditText(private val mcontext: Context, private val attrs: AttributeSet?, private val defStyleAttr:Int):RelativeLayout(mcontext,attrs,defStyleAttr) {
    constructor(mcontext: Context,attrs:AttributeSet?):this(mcontext,attrs,0)
    constructor(mcontext: Context):this(mcontext,null)
    init {
        val layoutInflater = mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        //val binding = DataBindingUtil.inflate<EtImgHintBinding>(layoutInflater,R.layout.et_img_hint,this,true)
        layoutInflater.inflate(R.layout.et_img_hint,this)

        val typedArray= mcontext.obtainStyledAttributes(attrs,R.styleable.ImageHintEditText)
        initTyped(typedArray)

    }
    private fun initTyped(typedArray:TypedArray){
        val text = typedArray.getString(R.styleable.ImageHintEditText_text_hint)
        tv.text = text
        val drawable = typedArray.getDrawable(R.styleable.ImageHintEditText_img_src_hint)
        drawable?.setBounds(0,0, drawable!!.intrinsicWidth, drawable!!.intrinsicHeight)
        tv.setCompoundDrawables(drawable,null,null,null)
        tv.compoundDrawablePadding = typedArray.getDimension(R.styleable.ImageHintEditText_drawable_padding,UIUitls.dp2px(7.0f).toFloat()).toInt()
        val paddingLeft = UIUitls.dp2px(typedArray.getDimension(R.styleable.ImageHintEditText_edit_padding_left,UIUitls.dp2px(7.0f).toFloat()))
        et.inputType = typedArray.getInt(R.styleable.ImageHintEditText_input_type,InputType.TYPE_CLASS_TEXT)
        et.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s!!.contains(" ")){
                    et.setText(s.toString().trim())
                    et.setSelection(start)
                }
            }

        })
        val underlineVisible = typedArray.getBoolean(R.styleable.ImageHintEditText_underLine_visible,true)
        line.visibility = if(underlineVisible) View.VISIBLE else View.INVISIBLE
        typedArray.recycle()
        val bigToSmallAnimation = AnimationUtils.loadAnimation(context,R.anim.anim_big_to_small)
        val smallToBigAnimation = AnimationUtils.loadAnimation(context,R.anim.anim_small_to_big)
        et.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
            run {
                if(et.text.toString().isEmpty()){
                    if (!hasFocus) {
                        tv.animate().translationY(UIUitls.dp2px(0.0f).toFloat()).duration = 200
                        tv.startAnimation(smallToBigAnimation)
                    } else {
                        tv.animate().translationY((-UIUitls.dp2px(25.0f)).toFloat()).duration = 200
                        tv.startAnimation(bigToSmallAnimation)
                    }
                }

            }
        }



    }


}